package com.example.viewpagertest2;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.ArrayList;

public class MyActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {
    /**
     * Called when the activity is first created.
     */

    private ViewPager viewPager;

    private PagerTabStrip tabStrip;

    private ArrayList<Fragment> fragmentList;

    private ArrayList<String> tabList;

    private RelativeLayout ruleLayout;

    private RelativeLayout mainLayout;

    private RelativeLayout.LayoutParams ruleParams;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ruleLayout = (RelativeLayout)findViewById(R.id.rule_layout);

        mainLayout = (RelativeLayout) findViewById(R.id.main_layout);

        fragmentList = new ArrayList<Fragment>();

        tabList = new ArrayList<String>();

        viewPager = (ViewPager)findViewById(R.id.view_pager);
        viewPager.setOnPageChangeListener(this);
        tabStrip = (PagerTabStrip)findViewById(R.id.pager_tab);

        //添加标题
        tabList.add("第一页");
        tabList.add("第二页");
        tabList.add("第三页");
        tabList.add("第四页");
        tabList.add("第五页");

        //添加Fragment
        fragmentList.add(new Fragment1());
        fragmentList.add(new Fragment2());
        fragmentList.add(new Fragment3());
        fragmentList.add(new Fragment4());
        fragmentList.add(new Fragment5());

        //设置标题栏的样式
        tabStrip.setDrawFullUnderline(false);
//        tabStrip.setTabIndicatorColor(Color.TRANSPARENT);
        tabStrip.setTabIndicatorColor(getResources().getColor(R.color.bl));
        //FragmentStatePagerAdapter继承这个的适配器可以销毁页卡
        ViewPagerFragmentAdapter adapter = new ViewPagerFragmentAdapter(getSupportFragmentManager(),fragmentList,tabList);

        //这个不会销毁页卡
//        ViewPagerFragmentAdpter2 adapter = new ViewPagerFragmentAdpter2(getSupportFragmentManager(),fragmentList,tabList);

        viewPager.setAdapter(adapter);

        viewPager.setCurrentItem(500);
        ruleParams = (RelativeLayout.LayoutParams) ruleLayout.getLayoutParams();
//        mainLayout.post(new Runnable() {
//            @Override
//            public void run() {
//                int ruleHeight1 = ruleLayout.getHeight();
//                Log.e("按钮布局的高度1：",""+ruleHeight1);
//
//                int height = tabStrip.getHeight();
//                Log.e("标题栏的高度：",""+height);
//                ruleParams.topMargin = height;
//                ruleLayout.setLayoutParams(ruleParams);
//
//                int tabPaddingBo = tabStrip.getPaddingBottom();
//                Log.e("标题栏底部原来的边距：",""+tabPaddingBo);
//                int tabPaddingTo = tabStrip.getPaddingTop();
//                Log.e("标题栏顶部原来的边距：",""+tabPaddingTo);
//
//                int mearHeight = ruleLayout.getMeasuredHeight();
//                Log.e("按钮布局的测量高度：",""+mearHeight);
//                int ruleHeight = ruleLayout.getHeight();
//                Log.e("按钮布局的高度：",""+ruleHeight);
//                tabStrip.setPadding(0,tabStrip.getPaddingTop(),0,ruleHeight+tabPaddingBo);
//
//                int tabPadBo = tabStrip.getPaddingBottom();
//                Log.e("标题栏底部的边距：",""+tabPadBo);
//            }
//        });

    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onPageSelected(int i) {
        Toast.makeText(MyActivity.this,"这是第"+(i+1)+"个页卡",Toast.LENGTH_LONG).show();
    }
}
